//
//  UserHelper.m
//  CarCare
//
//  Created by ileo on 15/1/7.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "UserHelper.h"
#import "NetUser.h"

#import "SystemHelper.h"
#import "Check.h"
#import "SettingMgr.h"
#import "CommentHelper.h"
#import "NetCaptcha.h"
#import "BaseWebViewController.h"
#import "AlertActionSheetMgr.h"

NSString * const USER_LOGIN = @"USER_LOGIN";
NSString * const USER_LOGOUT = @"USER_LOGOUT";
@interface UserHelper()

@end
@implementation UserHelper

+(void)getUserInfoSuccess:(void (^)())success fail:(void (^)())fail{
    
    [[[NetUser getUserInfo] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        
        NSDictionary *dic = JSON[@"infolist"];
        
        USERINFO.aliAccount = dic[@"ALIPAY_CODE"];
        USERINFO.deviceBindingTime = dic[@"BIND_TIME"];
        
        USERINFO.carIconImageURL = dic[@"BRANDIMG"];
        USERINFO.carBrand = dic[@"BRANDNAME"];
        USERINFO.carNo = dic[@"CARNO"];
        USERINFO.engineNO = dic[@"ENGINENO"];
        USERINFO.aliIDCard = dic[@"IDCARD"];
        USERINFO.headImageURL = dic[@"IMAGE"];
        USERINFO.aliIsBinding = ([dic[@"INFOFLAG"] integerValue] == 2);
        USERINFO.tel = dic[@"LOGINNAME"];
        USERINFO.vehicleId = [dic[@"VEHICLEID"] integerValue];
        USERINFO.carImageURL = dic[@"MODELIMG"];
        USERINFO.carModel = dic[@"MODELNAME"];
        USERINFO.aliRealName = dic[@"REALNAME"];
        USERINFO.carStyleID = [dic[@"STYLEID"] integerValue];
        USERINFO.carStyle = dic[@"STYLENAME"];
        USERINFO.userName = dic[@"USER_NAME"];
        USERINFO.pastDevice = dic[@"VEHICLE_DATA"];
        USERINFO.vinNO = dic[@"VIN"];
        USERINFO.carModelID = [dic[@"MODELID"] integerValue];
        if (![dic[@"FITFLAG"] isKindOfClass:[NSNull class]]) {
            USERINFO.fitFlag = [dic[@"FITFLAG"] integerValue];
        }
        
        if (success) {
            success();
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:USER_LOGIN object:nil];
        [SettingMgr sharedInstance];
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
    
    [[NetUser pushUserLocInfo] requestOnly];

}

+(void)loginWithTel:(NSString *)tel code:(NSString *)code success:(void (^)())success fail:(void (^)())fail{
    [[[NetUser loginWithTel:tel code:code] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        if (USERINFO.userID != 0 && USERINFO.userID != [JSON[@"userid"] integerValue]) {
            [USERINFO clearUserDefault];
        }
        USERINFO.isVisiter = NO;
        USERINFO.userID = [JSON[@"userid"] integerValue];
        USERINFO.session = JSON[@"session"];
                
        [UserHelper getUserInfoSuccess:success fail:fail];
        
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
}

+(void)loginByLCWithTel:(NSString *)tel pwd:(NSString *)pwd success:(void (^)())success fail:(void (^)())fail{
    [[[NetUser loginByLCWithTel:tel pwd:pwd] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        USERINFO.userID = [JSON[@"userid"] integerValue];
        USERINFO.session = JSON[@"session"];
        USERINFO.isVisiter = NO;
        [UserHelper getUserInfoSuccess:success fail:fail];
    } failure:fail];
}

+(void)loginOutSuccess:(void (^)())success fail:(void (^)())fail{
    
    [UserHelper visiterDisappearBuyCar];
    USERINFO.isLogin = NO;
    [CommentHelper stopTime];

    [[NSNotificationCenter defaultCenter] postNotificationName:USER_LOGOUT object:nil];
    [[NetUser loginOut] requestSuccess:^(id JSON) {
        if (success) {
            success();
        }
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
}

+(void)getCodeByTel:(NSString *)tel success:(void (^)())success fail:(void (^)())fail{
    [[[NetCaptcha obtainCodeFromTel:tel] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        [AlertMgr showShortSuccessTextTips:@[JSON[@"errMsg"]]];
        if (success) {
            success();
        }
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
}

+(void)callMessageCodeByTel:(NSString *)tel success:(void(^)())success fail:(void(^)())fail{
    [[[NetUser callMessageFromTel:tel] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        [AlertMgr showShortSuccessTextTips:@[JSON[@"errMsg"]]];
        if (success) {
            success();
        }
    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
}

#pragma mark - viiter

#define VISITER_TEL @"13917103046"
#define VISITER_CODE @"1111"

+(void)loginByVisiterSuccess:(void (^)())success fail:(void (^)())fail{
    
    [[[NetUser loginWithTel:VISITER_TEL code:VISITER_CODE] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
        if (USERINFO.userID != 0 && USERINFO.userID != [JSON[@"userid"] integerValue]) {
            [USERINFO clearUserDefault];
        }
        USERINFO.isVisiter = YES;
        USERINFO.userID = [JSON[@"userid"] integerValue];
        USERINFO.session = JSON[@"session"];
        [UserHelper getUserInfoSuccess:success fail:fail];

    } failure:^(id JSON) {
        if (fail) {
            fail();
        }
    }];
    
}

+(void)visiterShowBuyCar{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;

    [Factory buttonWithCenter:CGPointMake(__MAIN_WIDTH - 50,  __MAIN_HEIGHT - 100) withImage:ImageNamed(@"visiterExit") click:^{
        
    [ALERTACTIONSHEETMGR createFromVC:window.rootViewController withAlertType:TYPE_ALERTVC_ACTIONSHEET withTitle:@"" withMessage:@"" withCanCelTitle:@"取消" withRedBtnTitle:@"马上免费租盒" withOtherBtnTitle:@"放弃免费租盒权利，直接退出" clickIndexBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            [SCREEN clear];
            [SCREEN changeToLoginScreen];

            BaseWebViewController * webVC = [[BaseWebViewController alloc]initWithURL:@"http://m.ichezheng.com/mobile/mobile.html#/entry/select"];
            
            [webVC navSetupLeftTitle:@"关闭" action:^{
                [webVC dismissViewControllerAnimated:YES completion:nil];
            }];
            webVC.isScaleBool = NO;
            webVC.title = @"免费租盒";

            [((UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController) presentViewController:[UINavigationController navigationControllerWithRootViewController:webVC] animated:YES completion:nil  ];
        }else if(buttonIndex == 1)
        {
            //退出登录
            [SCREEN clear];
            [SCREEN changeToLoginScreen];
        }
    }];
        
    } onView:window].tag = 6765;

}

+(void)visiterDisappearBuyCar{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIView *view = obj;
        if (view.tag == 6765) {
            [view removeFromSuperview];
            *stop = YES;
        }
    }];
}


@end
